package cn.com.shadowless.xinning.view;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.ButterKnife;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.presenter.LoginPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.utils.WindowUtils;
import cn.jpush.android.api.JPushInterface;

/**
 * The type Base activity.
 *
 * @author sHadowLess
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected FragmentManager fragmentManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayout());
        JPushInterface.requestPermission(this);
        ButterKnife.bind(this);
        WindowUtils.hideStatusBar(this);
        fragmentManager = getSupportFragmentManager();
        setData();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("TAG", "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("TAG", "onStop: ");
    }

    @Override
    protected void onDestroy() {
        Log.e("TAG", "onDestroy: ");
        super.onDestroy();
    }


    /**
     * Sets layout.
     *
     * @return the layout
     */
    protected abstract int setLayout();

    /**
     * Sets data.
     */
    protected abstract void setData();

    /**
     * Init logout.
     *
     * @param userName the user name
     */
    protected void initLogout(String userName) {
        if (!"-1".equals(userName)) {
            LoginPresenter
                    .getSingleton()
                    .getLogoutInfo(userName, new RequestCallBack<RequestResult<Object>>() {
                        @Override
                        public void success(RequestResult<RequestResult<Object>> result) {
                            Log.e("TAG", "success: " + result.toString());
                            if (result.isSucceed()) {
                                PreferencesUtils.clear(MyApplication.getInstance());
                            }
                        }

                        @Override
                        public void fail(String errorMsg) {
                            ToastUtils.onErrorShowToast(errorMsg);
                        }
                    });
        }
    }
}